package sucre

class DirectivaPasswordController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ directivaPasswordInstanceList: DirectivaPassword.list( params ),
            directivaPasswordInstanceTotal: DirectivaPassword.count() ]
    }

    def show = {
        def directivaPasswordInstance = DirectivaPassword.get( params.id )

        if(!directivaPasswordInstance) {
            flash.message = "DirectivaPassword not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ directivaPasswordInstance : directivaPasswordInstance ]
        }
    }

    def delete = {
        def directivaPasswordInstance = DirectivaPassword.get( params.id )
        if(directivaPasswordInstance) {
            try {
                directivaPasswordInstance.delete(flush:true)
                flash.message = "DirectivaPassword ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "DirectivaPassword ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "DirectivaPassword not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def directivaPasswordInstance = DirectivaPassword.get( params.id )

        if(!directivaPasswordInstance) {
            flash.message = "DirectivaPassword not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ directivaPasswordInstance : directivaPasswordInstance ]
        }
    }

    def update = {
        def directivaPasswordInstance = DirectivaPassword.get( params.id )
        if(directivaPasswordInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(directivaPasswordInstance.version > version) {
                    
                    directivaPasswordInstance.errors.rejectValue("version",
                                "directivaPassword.optimistic.locking.failure",
                                '''Another user has updated this
                                DirectivaPassword while you were editing.''')
                    render(view:'edit',model:[
                           directivaPasswordInstance:directivaPasswordInstance])
                    return
                }
            }
            directivaPasswordInstance.properties = params
            if(!directivaPasswordInstance.hasErrors()
                && directivaPasswordInstance.save()) {
                flash.message = "DirectivaPassword ${params.id} updated"
                redirect(action:show,id:directivaPasswordInstance.id)
            }
            else {
                render(view:'edit',model:[
                        directivaPasswordInstance:directivaPasswordInstance])
            }
        }
        else {
            flash.message = "DirectivaPassword not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def directivaPasswordInstance = new DirectivaPassword()
        directivaPasswordInstance.properties = params
        return ['directivaPasswordInstance':directivaPasswordInstance]
    }

    def save = {
        def directivaPasswordInstance = new DirectivaPassword(params)
        if(!directivaPasswordInstance.hasErrors()
            && directivaPasswordInstance.save()) {
            flash.message = "DirectivaPassword ${directivaPasswordInstance.id} created"
            redirect(action:show,id:directivaPasswordInstance.id)
        }
        else {
            render(view:'create',
                model:[directivaPasswordInstance:directivaPasswordInstance])
        }
    }
}
